package dd.prog2.exercises.set11.a3;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class counter_v3{

	public static void main(String[] args) {
		new counter_v3().start();
	}

	JButton count;
	JButton reset;
	JButton close;
	JTextField textfield;
	
	public void start() {
		final int width = 215;
		final int bWidth = 69;
		JFrame frame = new JFrame("A1 simple counter");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(null);
		frame.setLocation(250, 250);
		frame.setResizable(false);
		frame.setSize(width, 180);

		Font f = new Font("SansSerif", Font.BOLD, 80);
		textfield = new JTextField("0");
		textfield.setLocation(0, 0);
		textfield.setSize(width,120);
		textfield.setEditable(false);
		textfield.setHorizontalAlignment(JTextField.CENTER);
		textfield.setFont(f);
		
		count = new JButton("Count");
		count.setLocation(0, 121);
		count.setSize(bWidth, 30);
		count.addActionListener(new ButtonListener());

		reset = new JButton("Reset");
		reset.setLocation(70, 121);
		reset.setSize(bWidth, 30);
		reset.addActionListener(new ButtonListener());
		
		close = new JButton("Exit");
		close.setLocation(140, 121);
		close.setSize(bWidth, 30);
		close.addActionListener(new ButtonListener());
		
		
		frame.getContentPane().add(textfield);
		frame.getContentPane().add(count);
		frame.getContentPane().add(reset);
		frame.getContentPane().add(close);
	
		frame.setVisible(true);
	}
	
	private class ButtonListener implements ActionListener {

		public ButtonListener() {}

		@Override
		public void actionPerformed(ActionEvent ae) {
			if(ae.getSource() == count){
				String text = textfield.getText();
				int z = Integer.parseInt(text);
				textfield.setText((++z)+"");
			} else if(ae.getSource() == reset){
				textfield.setText("0");
			} else if(ae.getSource() == close){
				System.exit(0);
			}
		}

	}

}